"""
Developed by Robin Bhattacharyya (memecache for GAE)

from gluon.contrib.gae_memcache import MemcacheClient
cache.ram=cache.disk=MemcacheClient(request)
"""

import time
from google.appengine.api.memcache import Client

class MemcacheClient(Client):
    def __init__(self, request):
        self.request=request
        Client.__init__(self)
    def __call__(self,key,f,time_expire=300):
        key='%s/%s' % (self.request.application,key)
        dt=time_expire
        value=None
        obj=self.get(key)
        if obj and obj[0]>time.time()-dt:
            value=obj[1]
        elif f is None:
            if obj: self.delete(key)
        else:
            value=f()
            self.set(key,(time.time(),value))
        return value
